package com.qiantu.phone.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.base.BaseDialog;
import com.hjq.http.lifecycle.ApplicationLifecycle;
import com.hjq.http.listener.HttpCallback;
import com.hjq.http.listener.OnHttpListener;
import com.qiantu.api.db.HandleListener;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.db.LLFloorDBManager;
import com.qiantu.api.db.LLRoomDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.FloorBean;
import com.qiantu.api.entity.HouseBean;
import com.qiantu.api.entity.RoomBean;
import com.qiantu.api.entity.RoomShareBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.event.RefreshControllerEvent;
import com.qiantu.phone.event.RefreshDeviceListEvent;
import com.qiantu.phone.event.RefreshFloorRoomEvent;
import com.qiantu.phone.ui.dialog.ConfirmCancelHintDialog;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class RoomShareListActivity  extends AppActivity {
    private RecyclerView mOtherHouseList;
    private RoomShareListActivity.RoomShareAdapter houseAdapter;
    private String roomSerialNo;
    private List<RoomShareBean> shareBeans;
    private SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.sss");
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        houseAdapter = new RoomShareListActivity.RoomShareAdapter();
        mOtherHouseList = findViewById(R.id.other_house_list);
        mOtherHouseList.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        mOtherHouseList.setAdapter(houseAdapter);
        houseAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                RoomShareBean bean = houseAdapter.getData().get(position);
                gotoShareView(bean);
            }
        });
        houseAdapter.addChildClickViewIds(R.id.cancel_share);
        houseAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                int id = view.getId();
                if (id == R.id.cancel_share) {
                    RoomShareBean bean = houseAdapter.getData().get(position);
                    showConfirmCancelHintDialog(bean,position);
                }
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_room_share_list;
    }

    @Override
    protected void initView() {
        roomSerialNo = getString("roomSerialNo");
    }

    @Override
    protected void initData() {
        LLHttpManager.getRoomShareList(new ApplicationLifecycle(), AppApplication.getInstance().getCurHouseInfo().getHouseSerialNo(), roomSerialNo, new HttpCallback<HttpData<Map<String,List<RoomShareBean>>>>(this) {
            @Override
            public void onSucceed(HttpData<Map<String,List<RoomShareBean>>> result) {
                shareBeans = result.getData().get("shareList");
                houseAdapter.setNewInstance(shareBeans);
            }

            @Override
            public void onFail(Exception e) {
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this,RoomShareInfoActivity.class);
        startActivity(intent);
    }

    @Override
    public void onRightClick(View view) {
        Intent intent = new Intent(this,RoomShareInfoActivity.class);
        intent.putExtra("roomSerialNo",roomSerialNo);
        startActivity(intent);
    }

    private BaseDialog mConfirmCancelHintDialog;
    private ConfirmCancelHintDialog.Builder mConfirmCancelHintDialogBuilder;
    private void showConfirmCancelHintDialog(RoomShareBean shareBean,int position){
        if(mConfirmCancelHintDialog == null){
            mConfirmCancelHintDialogBuilder = new ConfirmCancelHintDialog.Builder(getActivity());
            mConfirmCancelHintDialog = mConfirmCancelHintDialogBuilder.create();
        }
        mConfirmCancelHintDialogBuilder.setListener(new ConfirmCancelHintDialog.OnListener() {
            @Override
            public void onLeftClick(BaseDialog dialog) {
                mConfirmCancelHintDialogBuilder.dismiss();
            }

            @Override
            public void onRightClick(BaseDialog dialog) {
                mConfirmCancelHintDialogBuilder.dismiss();
                cancelShare(shareBean,position);
            }
        });
        mConfirmCancelHintDialogBuilder.setContent(String.format("该操作将会取消“%1$s”对该房间的操作授权？", shareBean.getName()));
        mConfirmCancelHintDialog.show();
    }

    private void cancelShare(RoomShareBean shareBean,int position){
        LLHttpManager.deleteRoomShare(new ApplicationLifecycle(), AppApplication.getInstance().getCurHouseInfo().getHouseSerialNo(), roomSerialNo,shareBean.getShareSerialNo(), new HttpCallback<HttpData<Void>>(this) {
            @Override
            public void onSucceed(HttpData<Void> result) {
                shareBeans.remove(position);
                houseAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFail(Exception e) {
            }
        });
    }

    private void gotoShareView(RoomShareBean bean){
        Intent intent = new Intent(this,RoomShareInfoActivity.class);
        intent.putExtra("roomSerialNo",roomSerialNo);
        intent.putExtra("data", (Serializable) bean);
        startActivity(intent);
    }

    private class RoomShareAdapter extends BaseQuickAdapter<RoomShareBean, BaseViewHolder> {

        public RoomShareAdapter() {
            super(R.layout.room_share_item);
        }

        @Override
        protected void convert(BaseViewHolder helper, RoomShareBean item) {
            helper.setText(R.id.user_name, item.getName());
            try {
                Date fromTime = simpleDateFormat1.parse(item.getFromTime());
                Date toTime = simpleDateFormat1.parse(item.getToTime());
                String time = "从"+ simpleDateFormat.format(fromTime) + "到" + simpleDateFormat.format(toTime);
                helper.setText(R.id.home_time, time);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
}